package com.braveman.im.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.braveman.im.R;
import com.braveman.im.api.Api;
import com.braveman.im.bean.GetFriendsInfoByIMid;
import com.braveman.im.bean.GetUserCommentsBean;
import com.braveman.im.ui.activity.message.ChatAllActivity;
import com.braveman.im.ui.activity.message.EditAddFriendsInforActivity;
import com.braveman.im.bean.PersonInfoDetailsBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.activity.message.FriendRemarkActivity;
import com.braveman.im.ui.activity.message.SelectFriendsGroupNameActivity;
import com.braveman.im.ui.weiget.ButtonItem;
import com.braveman.im.utils.UtilsStyle;
import com.bumptech.glide.Glide;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.UIUtils;
import com.vondear.rxtools.RxBarTool;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * 作者 昊昊.
 * 年份 2017/10/23.
 * 时间 19:38.
 * 项目名称 ZhengQiApp.
 */

/**
 * 好友的详细资料
 */
public class PersonInfoDetailsActivirt extends BaseActivity {
    private String friendId;
    private View mStatusBar;
    private ScrollView mSc;
    private ImageView mActivityImgFlockImg;
    private TextView mActivityTxtFlockTitle;
    private TextView mActivityTxtFlockContent;
    private TextView mActivityTxtFlockCaTegory;
    private TextView mActivityTxRegion;
    private TextView mActivityTxSignature;
    private ImageView mActivityImgLevel;
    private ButtonItem remarkBtn;
    private ImageView mActivityImgLevel1;
    private RelativeLayout mRlActivityMore;
    private ImageView mActivityDetailsNext;
    private TextView mActivityTxNumber;
    private Button mBtnActivityAddFriends;
    private boolean ifFriend;
    private String friendImUsername;
    private String friendId1;
    private String friendName;
    private ButtonItem mBtnActivityFriendsGroupName;
    private static final int PERSONGROUPNAME = 200;
    private String userFriendCategoryName;
    private String imFriendsId;
    private String userFriendCategoryId;
    private PersonInfoDetailsBean personInfoDetailsBean;
    private GetUserCommentsBean getUserCommentsBean;

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("详细资料").setLeftBtnIcon(R.mipmap.back_black).setOnLeftBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(PersonInfoDetailsActivirt.this);
            }
        });
    }

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initIntentData(Intent callIntent) {
        friendId = callIntent.getStringExtra("friendId");
        imFriendsId = callIntent.getStringExtra("imFriendsId");
    }

    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_preson_info_details;
    }

    @Override
    protected void initChildView() {
        mStatusBar = (View) findViewById(R.id.status_bar);
        mSc = (ScrollView) findViewById(R.id.sc);
        remarkBtn = findViewById(R.id.btn_activity_friends_remark);//设置备注及标签
        mActivityImgFlockImg = (ImageView) findViewById(R.id.activity_img_flock_img);
        mActivityTxtFlockTitle = (TextView) findViewById(R.id.activity_txt_flock_title);
        mActivityTxtFlockContent = (TextView) findViewById(R.id.activity_txt_flock_content);
        mActivityTxtFlockCaTegory = (TextView) findViewById(R.id.activity_txt_flock_caTegory);
        mActivityTxRegion = (TextView) findViewById(R.id.activity_tx_region);
        mActivityTxSignature = (TextView) findViewById(R.id.activity_tx_signature);
        mActivityImgLevel = (ImageView) findViewById(R.id.activity_img_level);
        mActivityImgLevel1 = (ImageView) findViewById(R.id.activity_img_level1);
        mRlActivityMore = (RelativeLayout) findViewById(R.id.rl_activity_more);
        mActivityDetailsNext = (ImageView) findViewById(R.id.activity_details_next);
        mActivityTxNumber = (TextView) findViewById(R.id.activity_tx_number);
        mBtnActivityAddFriends = (Button) findViewById(R.id.btn_activity_add_friends);
        mBtnActivityFriendsGroupName = findViewById(R.id.btn_activity_friends_group_name);
    }

    @Override
    protected void initChildData() {
        if (TextUtils.isEmpty(friendId)) {
            ArrayList<String> list = new ArrayList<>();
            list.add(imFriendsId);
            personInfoDetailsBean = new PersonInfoDetailsBean();
            ApiManager.getApi().getFriendInfoByIMid(list).enqueue(new NetCallBack<List<GetFriendsInfoByIMid>>() {
                @Override
                protected void onSuccess(Call<List<GetFriendsInfoByIMid>> call, Response<List<GetFriendsInfoByIMid>> response) {
                    if (response != null && response.body() != null && response.body().size() == 1) {
                        friendId = response.body().get(0).id;

                        if (TextUtils.isEmpty(friendId)) {
                            UIUtils.showToast("好友id为空");
                            return;
                        }
                        doNetDateWork(friendId);
                    }
                }
            });
        } else {
            doNetDateWork(friendId);
        }
       // getUserCommentsBean = new GetUserCommentsBean();//获取用户用户备注
//        ApiManager.getApi().getUserComments(friendId).enqueue(new NetCallBack<GetUserCommentsBean>() {
//            @Override
//            protected void onSuccess(Call<GetUserCommentsBean> call, Response<GetUserCommentsBean> response) {
//                getUserCommentsBean = response.body();
//                if (response != null) {
//                    remarkBtn.tv_title_del.setText(personInfoDetailsBean.friendRemark);//设置备注
//                }
//            }
//        });
    }

    private void doNetDateWork(String id) {

        ApiManager.getApi().getPersonInfoDetails(id).enqueue(new NetCallBack<PersonInfoDetailsBean>() {
            PersonInfoDetailsBean personInfoDetailsBean = new PersonInfoDetailsBean();

            @Override
            protected void onSuccess(Call<PersonInfoDetailsBean> call, Response<PersonInfoDetailsBean> response) {
                if (response != null) {
                    personInfoDetailsBean = response.body();
                    if (personInfoDetailsBean != null) {
                        Glide.with(PersonInfoDetailsActivirt.this).load(personInfoDetailsBean.friendAvatar).into(mActivityImgFlockImg);
                        mActivityTxtFlockTitle.setText(TextUtils.isEmpty(personInfoDetailsBean.friendRemark) ? personInfoDetailsBean.friendRemark : personInfoDetailsBean.friendName);
                        mActivityTxtFlockTitle.setText("诚信号：" + personInfoDetailsBean.friendSn);
                        mActivityTxtFlockCaTegory.setText(personInfoDetailsBean.tagNames);
                        mActivityTxRegion.setText(personInfoDetailsBean.friendHometown);//好友老家
                        mActivityTxSignature.setText(personInfoDetailsBean.tagNames);
                        Drawable drawable = getDrawableForMine(personInfoDetailsBean.friendIntegrityIndex);
                        mActivityImgLevel.setImageDrawable(drawable);
                        Drawable drawable1 = getDrawableForMine(personInfoDetailsBean.friendMinIntegrityIndex);
                        mActivityImgLevel1.setImageDrawable(drawable1);
                        ifFriend = personInfoDetailsBean.ifFriend;
                        mBtnActivityAddFriends.setText(personInfoDetailsBean.ifFriend ? "发消息" : "添加到通讯录");
                        friendImUsername = personInfoDetailsBean.friendImUsername;
                        friendName = personInfoDetailsBean.friendName;
                        userFriendCategoryName = personInfoDetailsBean.userFriendCategoryName;
                        mBtnActivityFriendsGroupName.tv_title_del.setText(userFriendCategoryName);
                        remarkBtn.tv_title_del.setText(personInfoDetailsBean.friendRemark);//设置备注
                        userFriendCategoryId = personInfoDetailsBean.userFriendCategoryId;
                    }

                }
            }
        });
    }

    private Drawable getDrawableForMine(int positon) {
        Drawable drawable = getResources().getDrawable(R.mipmap.star_level_1);

        switch (positon) {
            case 1:
                drawable = getResources().getDrawable(R.mipmap.star_level_1);
                break;
            case 2:
                drawable = getResources().getDrawable(R.mipmap.star_level_2);
                break;
            case 3:
                drawable = getResources().getDrawable(R.mipmap.star_level_3);
                break;
            case 4:
                drawable = getResources().getDrawable(R.mipmap.star_level_4);
                break;
            case 5:
                drawable = getResources().getDrawable(R.mipmap.star_level_5);
                break;
            default:

        }
        return drawable;
    }

    @Override
    protected void setChildViewListener() {
        findViewById(R.id.rl_activity_more).setOnClickListener(this);
        findViewById(R.id.btn_activity_add_friends).setOnClickListener(this);
        mBtnActivityFriendsGroupName.setOnClickListener(this);
        remarkBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        Bundle bundle = new Bundle();
        switch (v.getId()) {
            case R.id.rl_activity_more:
                bundle.putString("imFriendsId", imFriendsId);
                ActivityUtils.startActivity(PersonInfoDetailsActivirt.this, PersonalDataActivity.class, bundle);
                break;
            case R.id.btn_activity_add_friends:
                if (ifFriend) {
                    bundle.putString("user_id", friendImUsername);
                    bundle.putInt("states", 3);
                    ActivityUtils.startActivity(PersonInfoDetailsActivirt.this, ChatAllActivity.class, bundle);
                } else {
                    bundle.putString("friendId", friendId);
                    bundle.putString("friendName", friendName);
                    ActivityUtils.startActivity(PersonInfoDetailsActivirt.this, EditAddFriendsInforActivity.class, bundle);
                }
                break;
            case R.id.btn_activity_friends_group_name:
                Bundle bundle1 = new Bundle();
                bundle1.putString("userFriendCategoryName", userFriendCategoryName);
                bundle1.putString("userFriendCategoryId", userFriendCategoryId);
                bundle1.putString("friendId", friendId);
                ActivityUtils.startActivityForResult(PersonInfoDetailsActivirt.this, SelectFriendsGroupNameActivity.class, bundle1, PERSONGROUPNAME);
                break;
            case R.id.btn_activity_friends_remark: //设置备注及标签:
                bundle.putString("friendId", friendId);
                ActivityUtils.startActivity(this, FriendRemarkActivity.class, bundle);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PERSONGROUPNAME:
                    if (data != null) {
                        String name = data.getStringExtra("name");
                        mBtnActivityFriendsGroupName.tv_title_del.setText(name);
                    }
                    break;
            }
        }
    }
}
